package com.clouddot.baipai.ui.fragment;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.ui.util.ToastUtil;
import com.clouddot.baipai.ui.util.asy.AsyCallbackListener;
import com.clouddot.baipai.ui.util.asy.AsyExecuteListener;
import com.clouddot.baipai.ui.util.asy.AsyHelper;

/**
 * Created by wxy on 2018/4/12.
 */

public class BaseFragment extends Fragment {
    public Animation animation1,animation2,loadingAnim,animation3,animation4;
    public ColorStateList blue,gray;

    public Activity mainActivity ;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        animation2 = AnimationUtils.loadAnimation(activity, R.anim.buttom_start_anim);
        animation1 = AnimationUtils.loadAnimation(activity,R.anim.buttom_start_anim2);
        animation4 = AnimationUtils.loadAnimation(activity,R.anim.buttom_start_anim4);
        animation3 = AnimationUtils.loadAnimation(activity,R.anim.buttom_click_anim);
        loadingAnim = AnimationUtils.loadAnimation(activity,R.anim.rotate);
        LinearInterpolator lir = new LinearInterpolator();
        loadingAnim.setInterpolator(lir);

        animation1.setDuration(500);
        animation2.setDuration(500);
        animation3.setDuration(500);
        animation4.setDuration(500);

        Resources resource = getActivity().getBaseContext().getResources();
        blue = resource.getColorStateList(R.color.yellow);
        gray = resource.getColorStateList(R.color.gray);

        mainActivity = getActivity();



    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView();// 设置布局

        initControls();// 初始化控件

        setListener();// 设置控件监听

        loadParams();// 加载传递的参数
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected void setContentView() {

    }

    protected void initControls() {

    }

    protected void setListener() {

    }

    protected void loadParams() {

    }

    public void showLoading() {

    }

    public void closeLoading() {

    }

    public void runThreadAction() {
        AsyHelper.create(new AsyExecuteListener() {
                             public Object run() throws Exception {
                                 return runThreadActionExceute();
                             }

                         },

                new AsyCallbackListener() {
                    public void run(Object result) {
                        runThreadActionSuccess(result);
                    }

                    public void error(Exception ex) {
                        runThreadActionError(ex);
                    }
                });
    }

    protected Object runThreadActionExceute() {
        showLoading();
        return null;
    }

    protected void runThreadActionSuccess(Object result) {
        closeLoading();
    }

    protected void runThreadActionError(Exception ex) {
        closeLoading();
        ToastUtil.showToast(Env.getContent(), ex.getMessage(), 3000);
    }

    public void runThreadAction(String actionName) {
        final String name = actionName;
        AsyHelper.create(new AsyExecuteListener() {
                             public Object run() throws Exception {
                                 return runThreadActionExceute(name);
                             }

                             ;
                         },

                new AsyCallbackListener() {
                    public void run(Object result) {
                        runThreadActionSuccess(name, result);
                    }


                    public void error(Exception ex) {
                        runThreadActionError(name, ex);
                    }
                });
    }

    protected Object runThreadActionExceute(String actionName) {
        showLoading();
        return null;
    }

    protected void runThreadActionSuccess(String actionName, Object result) {
        closeLoading();
    }

    protected void runThreadActionError(String actionName, Exception ex) {
        closeLoading();
        ToastUtil.showToast(Env.getContent(), ex.getMessage(), 3000);
    }


}
